/*
 * @Author: 晟松
 * @Date: 2021-12-08 23:45:29
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-08 20:26:02
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\controller\CommunityController.js
 * @Description:
 */
import Router from "koa-router";
import koaBodyparser from "koa-bodyparser";
import koaBody from "koa-body";
import CommunityService from "@main/service/CommunityService";
import allowUserIdentity from "@main/middleware/routeMid/validateIdentity";
import { LoginError } from "@main/common/CommonError";

let communityRouter = new Router({
    prefix: "/api/community",
});
/**
 * @description: 居委会审核居民
 * @param {*}
 * @return {*}
 */
communityRouter.post(
    "/communityReview",
    koaBodyparser(),
    allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(
            await CommunityService.communityReview(params)
        );
    }
);

communityRouter.post(
    "/getAllResident",
    koaBodyparser(),
    allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(
            await CommunityService.getAllResident(params)
        );
    }
);

communityRouter.post(
    "/searchAllResident",
    koaBodyparser(),
    allowUserIdentity("community"),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(
            await CommunityService.searchAllResident(params)
        );
    }
);
/**
 * @description: 居民批量注册
 * @param {*}
 * @return {*}
 */
communityRouter.post(
    "/batchRegisterResident",
    koaBody({
        multipart: true, //支持fordata表单上传文件
        formidable: {
            // 这里还有一些其他的配置，需要时去官网文档看
            maxFileSize: 2000 * 1024 * 1024, // 设置上传文件大小最大限制，默认2M
        },
    }),
    async (ctx) => {
        // files.file里面的 .file是在formdata表单中指定文件的属性名，可自定义
        let files = ctx.request.files.file;
        // console.log("files: ", files);
        let params = ctx.request.body;
        // console.log("params: ", params);
        ctx.body = ctx.renderJson(
            await CommunityService.batchRegisterResident(files, params)
        );
    }
);

export default communityRouter;
